package com.jrx.anytxn.reporting.job.unionFile.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.config.UnionFileConfig;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.job.unionFile.step.CompressUnionFileListener;
import com.jrx.anytxn.reporting.service.IUnionFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 按照指定路径生成压缩文件
 */
@Configuration
@EnableConfigurationProperties(UnionFileConfig.class)
public class CompressUnionFileConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(CompressUnionFileConfiguration.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private UnionFileConfig unionFileConfig;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private IUnionFileService unionFileService;
    @Autowired
    private CompressUnionFileListener compressUnionFileListener;

    @Bean
    public Job compressUnionFileJob(){
        return jobBuilderFactory.get("compressUnionFileJob")
                .listener(compressUnionFileListener)
                .start(compressUnionFileStep())
                .build();
    }

    @Bean
    public Step compressUnionFileStep() {
        return stepBuilderFactory.get("compressUnionFileStep")
                .tasklet(compressUnionFileTasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet compressUnionFileTasklet() {
        return (stepContribution, chunkContext) -> {
            logger.debug("压缩文件开始。。。。。。");
            String filePath = unionFileConfig.getUnionFilePath();//原文件目录

            String outFilePath = unionFileConfig.getOutUnionFilePath();//压缩文件目录

            String nftsPath = unionFileConfig.getNftsPath();//文件传输路径

            PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
            unionFileService.compressFile(org,filePath,outFilePath,nftsPath);

            logger.debug("压缩文件完成");
            return RepeatStatus.FINISHED;
        };
    }
}
